package com.juyu.miliao.scheduler.dao.mysql;

import com.juyu.miliao.common.dataSource.tool.distributeOperate.DistributeOperate;
import com.juyu.miliao.common.dataSource.tool.operate.Operate;
import com.juyu.miliao.common.util.CommonUtil;
import com.juyu.miliao.scheduler.domin.UserVideo;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author jh
 * 2018/7/23
 */
@Repository
public class HomeVideoDao {

    @Resource
    private SqlSession sqlSession;

    @DistributeOperate(ds = "miliao-user")
    public List<UserVideo> listLikeVideo(Integer likeSize, Set<String> sortSet) {
        Map<String, Object> map = new HashMap<>(4);
        map.put("likeSize", likeSize);
        map.put("sortSet", sortSet);
        return sqlSession.selectList("listLikeVideo", map);
    }

    /**
     * 此方法会自动排序
     *
     * @param videoIds videoIds
     * @return list
     */
    @DistributeOperate(value = "strategy('byVideoId', #videoIds)")
    public List<UserVideo> listSortVideo(List<Long> videoIds) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("videoIds", videoIds);
        return sqlSession.selectList("listSortVideo", map);
    }

    @Operate("strategy('byVideoId', #videoId)")
    public UserVideo getUserVideo(Long videoId) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("videoId", videoId);
        return sqlSession.selectOne("getUserVideo", map);
    }

    @DistributeOperate(ds = "miliao-user")
    public List<UserVideo> listNewVideo(Integer timeSize, Set<String> sortSet) {
        Map<String, Object> map = new HashMap<>(4);
        map.put("timeSize", timeSize);
        map.put("sortSet", sortSet);
        return sqlSession.selectList("listNewVideo", map);
    }

    /**
     * 根据videoIds查询,结果附带user
     *
     * @param videoIds videoIds
     * @return List<UserVideo>
     */
    @DistributeOperate("strategy('byVideoId', #videoIds)")
    public List<UserVideo> listVideoWithUser(List<Long> videoIds) {
        if (CommonUtil.isNotEmptyCollection(videoIds)) {
            Map<String, Object> map = new HashMap<>(2);
            map.put("videoIds", videoIds);
            return sqlSession.selectList("listVideoWithUser", map);
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 根据videoId查询,结果附带user
     *
     * @param videoId videoId
     * @return UserVideo
     */
    @Operate("strategy('byVideoId', #videoId)")
    public UserVideo getVideoWithUser(Long videoId) {
        if (videoId == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<>(2);
        map.put("videoId", videoId);
        return sqlSession.selectOne("getVideoWithUser", map);
    }

}
